$(function () {
    var s = $(".all-function:checked").length;
    var sum = $(".all-function").length;
    if(s == sum){
        $("#all-function").iCheck('check');
    }
   $("#all-function").on("ifClicked",function (ev) {//选中
       if(ev.target.checked){
           $("input[name='functionIds']").iCheck('uncheck');
       }else{
           $("input[name='functionIds']").iCheck('check');
       }
   });

   //第一级
   $(".parent-function").on('ifClicked',function (ev) {//选中
       var parentEm = $($(this).parents('.parent-function-box')[0]);
       if(ev.target.checked){
           parentEm.next('.child-function-box').find("input:checkbox").iCheck('uncheck');
       }else{
           parentEm.next('.child-function-box').find("input:checkbox").iCheck('check');
       }
   });

   //第二级
    $(".child-function-two").on('ifClicked',function (ev) {//选中
        var parentEm = $($(this).parents('label.show')[0]);
        //二级总数量
        var p = $(parentEm.parent('div'));
        var selectCount = p.find(".child-function-two:checked").length;
        if(ev.target.checked){
            parentEm.find("div.three-level-child input:checkbox").iCheck('uncheck');
            selectCount = selectCount -1;
        }else{
            parentEm.find("div.three-level-child input:checkbox").iCheck('check');
            selectCount = selectCount + 1;
        }
        var prevEm = $($(this).parents('div.child-function-box')[0]).prev('label');
        if(selectCount==0){
            $(prevEm).find('.parent-function').iCheck('uncheck');
        }
        if(selectCount > 0){
            $(prevEm).find('.parent-function').iCheck('check');
        }
    });

    //三级
    $(".child-function-three").on('ifClicked',function (ev) {
        var parentEm = $($(this).parents('label')[0]);
        var p = $(parentEm.parent('div'));
        var selectCount = p.find(".child-function-three:checked").length;
        if(ev.target.checked){
            selectCount = selectCount - 1;
        }else{
            selectCount = selectCount + 1;
        }
        var prevEm = $($(this).parents('div.three-level-child')[0]).prev('div');
        var c = 0;
        if(selectCount==0){
            $(prevEm).find('.child-function-two').iCheck('uncheck');
        }
        if(selectCount > 0){
            c = 1;
            $(prevEm).find('.child-function-two').iCheck('check');
        }
        var siblingsList = prevEm.parent('label.show').siblings();
        var twoSum = 0;
        $.each(siblingsList,function (index,em) {
            twoSum = twoSum + $(em).find(".child-function-two:checked").length;
        });
        twoSum = twoSum + c;
        prevEm = $(prevEm.parents(".child-function-box")[0]).prev('label');
        if(twoSum == 0){
            prevEm.find("input:checkbox").iCheck('uncheck');
        }
        if(twoSum > 0){
            prevEm.find("input:checkbox").iCheck('check');
        }
    });

    //所有的权限
    $(".all-function").on('ifClicked',function (ev) {
        var selectCount = $("input[name='functionIds']:checked").length;
        if(ev.target.checked){
            selectCount = selectCount - 1;
        }else{
            selectCount = selectCount + 1;
        }
        var sum = $(".all-function").length;
        if(sum==selectCount){
            $("#all-function").iCheck('check');
        }else{
            $("#all-function").iCheck('uncheck');
        }
    });
});